using System;
using System.Collections.Generic;
using Admin3000.Backend.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using MyWebApi.Api.ParamModel;
using Newtonsoft.Json;
using MyWebApi.Api.Repository;


namespace MyWebApi.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private Api.Db.MyWebApiDb db=new Api.Db.MyWebApiDb();
        private IRepository<Users> _usersRespository=new EfRepository<Users>();

        [HttpGet]
        public dynamic Get()
        {

            var users = _usersRespository.Table.ToList();
            return new {
                Code =1000,
                Data =users,
                Msg ="获取用户列表成功^_^"
            };
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var user = db.Users.Where(x => x.Id == id).FirstOrDefault();
            return user;
        }

        [HttpPost]
        public string Post(NewUser model)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            // settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            var user = new Users
            {
                Username = model.Username,
                Password = model.Password
            };

            db.Users.Add(user);
            db.SaveChanges();

            var res = new
            {
                Code = 1000,
                Data = user,
                Msg = "创建用户成功"
            };
            return JsonConvert.SerializeObject(res, settings);
        }

        [HttpPut("{id}")]
        public dynamic Put(int id, NewUser model)
        {
            var user = db.Users.Where(x => x.Id == id).FirstOrDefault();

            if (user != null)
            {
                user.Username = model.Username;
                user.Password = model.Password;
                user.UpdatedTime=DateTime.Now;
                db.Update(user);
                db.SaveChanges();
                return new
                {
                    Code = 1000,
                    Data = user,
                    Msg = string.Format("你修改的用户的id为：{0}，已经修改成功，请注意查收", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                };
            }
        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var user = db.Users.Where(x => x.Id == id).FirstOrDefault();
            if (user != null)
            {
                db.Users.Remove(user);
                db.SaveChanges();
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除指定id为{0}的用户成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                };
            }

        }

        private IEnumerable<Users> GetUsers()
        {
            var users =
                new List<Users> {
                    new Users {
                        Id = 1,
                        Username = "admin",
                        Password = "芝麻开门"
                    },
                    new Users {
                        Id = 2,
                        Username = "超级管理员",
                        Password = "芝麻开门"
                    },
                    new Users {
                        Id = 3,
                        Username = "普通用户",
                        Password = "芝麻开门"
                    }
                };

            return users;
        }
    }
}
